package org.cycuchy.kapsel
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	import mx.containers.Canvas;

	public class Test extends Canvas {
		
		
		private var _bitmap:BitmapData;
		
		private var cnt:int;
		private var ii:int = 0;
		private var v:Array = new Array();
				
		public function Test()
		{
			super();
            _bitmap = new BitmapData(600, 1200, false, 0x000000FF);
            var image:Bitmap = new Bitmap(_bitmap);
        	addChild(image);
			var s:String;
			cnt = 33;
			for (var i:int=1; i<cnt; i++) {
				if (i < 10) {
					s = "0";
				} else {
					s = "";
				}
				s += i;
				var loader:Loader = new Loader();
    	        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
    	        loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, iOError);
        	    loader.load(new URLRequest("assets/kapsle/" + s + ".jpg"));
			}
		}
		
		private function iOError(event:IOErrorEvent):void {
			trace(event);
		}
		
		public function onLoad(event:Event):void {
			trace(LoaderInfo(event.target).url);
            //var loaderBmp:Bitmap = Bitmap(LoaderInfo(event.target).content);
            ii++;
            //v.push(Bitmap(LoaderInfo(event.target).content));
            trace("loaded: ", ii);
//            if (ii == cnt - 1) {
            	createBitmap(Bitmap(LoaderInfo(event.target).content));
//            }
//            var _bitmap:BitmapData= new BitmapData(200,200,false, 0x000000FF);
//            var image:Bitmap = new Bitmap(_bitmap);
//            
//            var rect:Rectangle = new Rectangle(150, 150, 50, 50);
//            var pt:Point;
//            for(var i:int = 0; i < 4; i++) {
//	            pt = new Point(0, i * 50);
//	            _bitmap.copyPixels(loaderBmp.bitmapData, rect, pt);
//            }
//            addChild(image);			
            
        }
        
        private var xx:int = 0;
        private var yy:int = 0;
        
        private function createBitmap(bmp:Bitmap):void {
        	trace("cb");
            var rect:Rectangle = new Rectangle(0, 0, 150, 150);
            var pt:Point;
//        	for each (var bmp:Bitmap in v) {
        		if (xx == 4) {
        			xx = 0;
        			yy++;
        		}
	            pt = new Point(xx * 150, yy * 150);
	            _bitmap.copyPixels(bmp.bitmapData, rect, pt);
	            xx++;
//        	}
        }
        
	}
}